var Project = {
    tableId: "projectTable"
};
var numberPage = 1;
layui.use(['table', 'admin', 'ax', 'form', 'layer', 'jquery','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;

    laydate.render({
        elem: '#kyriqi'
        ,range: true
    });


    //dd
    var ajax1 = new $ax(Feng.ctxPath + "/project/selectDdListAll", function (data) {
        var sb;
        for(let i = 0; i < data.length; i++) {
            sb+='<option value="'+data[i].tel+'">' + data[i].dd + '</option>';
        }
        $("#dd").append(sb);
        form.render();
    }, function (data) {
        Feng.error(data.responseJSON.message);
    });
    ajax1.start();

    /**
     * 初始化表格的列
     */
    Project.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers', title: '序号', align: "center", fixed: true, width: 46},
            {field: 'name', sort: true, title: '工程名称', align: "left", fixed: true, width: 260},
            {field: 'ddname', sort: false, title: '督导', align: "left"},
            {field: 'dd', sort: true, title: '督导电话', align: "left"},
            {title: '开店状态', align: "left", templet:'#statusTpl'},
            {field: 'kyriqi', sort: true, title: '开业日期', align: "left",templet:function (d) {
                    if(d.kyriqi!=null && d.kyriqi!='' && d.kyriqi!=undefined){
                        return d.kyriqi.substring(0,10);
                    }else {
                        return d.kyriqi;
                    }
                }},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 120, fixed: 'right'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Project.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        queryData['dd'] = $("#dd").val();
        queryData['status'] = $("#status").val();
        var str = $("#kyriqi").val();
        if(str!='' && str!=null){
            var ary=str.split(" - ");
            queryData['beginTime'] =ary[0];
            queryData['endTime'] = ary[1];
        }
        table.reload(Project.tableId, {
            where: queryData, page: {curr: 1}
        });
        // Project.tableCfg.url=Feng.ctxPath + '/project/wsjdReportList';
        // Project.tableCfg.where=queryData;
        // table.render(Project.tableCfg);
        // sessionStorage.setItem("wsjdReport",JSON.stringify(queryData));
    };


    /**
     * 渲染表格
     */
    Project.tableCfg={
        elem: '#' + Project.tableId,
        page: true,
        url:Feng.ctxPath + '/project/wsjdReportList',
        cellMinWidth: 100,
        even: true,
        cols: Project.initColumn(),
        where:{status:$("#status").val()},
        done: function (res, page, count) {
            if ($("#pageA").val() != null && $("#pageA").val() != "" && numberPage == 1) {
                page = $("#pageA").val();
                numberPage = 0;
                table.reload(Project.tableId, {page: {curr: page}}, function () {
                    $("#pageA").val("");
                });
            }
            var number = $("#changeDM").children().length;
            let tableView = this.elem.next();
            layui.each(res.data, function (i, item) {
                if (number == 0) {
                    tableView.find('tr[data-index=' + i + ']').find('td').data('edit', false);
                }
            })
            $(".layui-table-main tr").each(function (index ,val) {
                $($(".layui-table-fixed .layui-table-body tbody tr")[index]).height($(val).height());
            });
        }
    };
    /*var  para = sessionStorage.getItem("wsjdReport");
    if(para!=null){
        Project.tableCfg.url=Feng.ctxPath + '/project/wsjdReportList';
        var p=JSON.parse(para);
        Project.tableCfg.where=p;
        if(p.name!=''){
            $('#name').val(p.name);
        }
        if(p.dd!=''){
            $('#dd').val(p.dd);
        }
        if(p.beginTime!='' && p.endTime!='' && p.beginTime!=undefined &&p.endTime!=undefined){
            $('#kyriqi').val(p.beginTime + ' - '+p.endTime);
        }
        if(p.status!=''){
            $('#status').val(p.status);
        }
        form.render('select');
    }*/
    table.render(Project.tableCfg);
    /**
     * 搜索按钮点击事件
     */
    $('#btnSearch').click(function () {
        Project.search();
    });


    /**
     * 导出excel
     */
    $('#export').click(function () {
        Project.exportExcel();
    });

    /**
     * 取消网上建店
     * @param data 点击按钮时候的行数据
     */
    Project.rebut = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/taskStatus/deleteWsjd", function (data) {
                Feng.success("取消网上建店成功!");
                table.reload(Project.tableId);
            }, function (data) {
                Feng.error("取消网上建店失败!" + data.responseJSON.message + "!");
            });
            ajax.set("proId", data.id);
            ajax.start();
        };
        Feng.confirm("是否取消网上建店?", operation);
    };


    //工具条点击事件
    table.on('tool(' + Project.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'rebut') {
            Project.rebut(data);
        }
    });

});
